package com.demo.rocketmq.controller;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wangfengchen
 */
@Slf4j
public class Producer {

  public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
    DefaultMQProducer mqProducer = new DefaultMQProducer("mpc_plain_short_message_group_local");
//        mqProducer.setNamesrvAddr("124.222.223.222:9876;47.96.232.192:9876");
    mqProducer.setNamesrvAddr("rocketmq-12.test.newhuoidc.com:9876");
    mqProducer.start();
    // 消息不超过4MB
    String topic = "mpc_plain_short_message_topic_local";
    List<Message> messages = new ArrayList<>(3);
    messages.add(new Message(topic, "*", IdUtil.getSnowflakeNextIdStr(), "Hello World-1".getBytes()));
    messages.add(new Message(topic, "*", IdUtil.getSnowflakeNextIdStr(), "Hello World-2".getBytes()));
    messages.add(new Message(topic, "*", IdUtil.getSnowflakeNextIdStr(), "Hello World-3".getBytes()));
    SendResult sendResult = mqProducer.send(messages);
    log.info("sendResult = {}", JSON.toJSONString(sendResult));
    mqProducer.shutdown();
  }
}
